package Zoo.handler;


/**
 * 各个用户登录界面的处理类
 */

import Zoo.entity.*;
import Zoo.impl.*;
import Zoo.service.*;
import Zoo.view.*;
import Zoo.view.leaderview.LeaderView;
import Zoo.view.leaderview.LeaderView1;
import Zoo.view.registerview.RegisterView;

import Zoo.view.userview.UserView;
import Zoo.view.zoomanview.ZoomenView;
import Zoo.view.zoomanview.ZoomenView1;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.sql.SQLException;

public class LoginHandler extends KeyAdapter implements ActionListener {
    private Loginview loginview;

    public LoginHandler(Loginview loginview) {
        this.loginview = loginview;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("重置".equals(text)) {
            loginview.getUsertxt().setText("");
            loginview.getPwdField().setText("");
        } else if ("登录".equals(text)) {
            try {
                //检查有没有选身份
                check();
                //选了不同身份，则调用不同身份的登录方法
                if (loginview.getFlag() == 1) {
                    loginUser();
                } else if (loginview.getFlag() == 2) {
                    loginVisitor();
                } else if (loginview.getFlag() == 3) {
                    loginLeader();
                } else if (loginview.getFlag() == 4) {
                    loginZoomen();
                }

            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } else if ("注册".equals(text)) {
            new RegisterView();
            loginview.dispose();
        }
    }

    //
    private void check() {
        String user = loginview.getUsertxt().getText();
        char[] chars = loginview.getPwdField().getPassword();
        //校验密码和用户名是否为空，是否选择身份
        if (user == null || "".equals(user.trim()) || chars == null || loginview.getFlag() == 0) {
            JOptionPane.showMessageDialog(loginview, "用户名和密码必填，身份必选");
        }

    }

    private void loginUser() throws SQLException {
        String user = loginview.getUsertxt().getText();
        char[] chars = loginview.getPwdField().getPassword();
        UserEntity userEntity = new UserEntity();

        //校验密码和用户名是否为空
        if (user == null || "".equals(user.trim()) || chars == null || loginview.getFlag() == 0) {
            JOptionPane.showMessageDialog(loginview, "用户名和密码必填，身份必选");
            return;
        }

        String pwd = new String(chars);
        System.out.println(user + ":" + pwd);

        //用户登录
        UserService userService = new UserServiceImpl();

        userEntity.setUsername(user);
        userEntity.setPwd(pwd);
        boolean flag = userService.validateUser(userEntity);
        if (flag) {

            //跳转到用户的主界面并销毁登录界面
            //把当前登录用户的用户名传过去主界面
            String userName = loginview.getUsertxt().getText();
            new UserView(userName);


            loginview.dispose();

        } else {
            JOptionPane.showMessageDialog(loginview, "用户名和密码错误");
        }
    }


    private void loginVisitor() throws SQLException {
        String visitorName = loginview.getUsertxt().getText();
        char[] chars = loginview.getPwdField().getPassword();

        //校验密码和用户名是否为空
        if (visitorName == null || "".equals(visitorName.trim()) || chars == null || loginview.getFlag() == 0) {
            JOptionPane.showMessageDialog(loginview, "用户名和密码必填，身份必选");
            return;
        }

        String idNumber = new String(chars);
        System.out.println(visitorName + ":" + idNumber);

        //参观者登录
        VisitorService visitorService = new VisitorServiceImpl();
        VisitorEntity visitorEntity = new VisitorEntity();
        visitorEntity.setVisitorName(visitorName);
        visitorEntity.setIdNumeber(idNumber);
        boolean flag = visitorService.validateVisitor(visitorEntity);


        if (flag) {
            //跳转到参观者的主界面并销毁登录界面

            new VisitorView();
            loginview.dispose();

        } else {
            JOptionPane.showMessageDialog(loginview, "用户名和密码错误");
        }
    }

    private void loginZoomen() throws SQLException {
        String zoomenName = loginview.getUsertxt().getText();
        char[] chars = loginview.getPwdField().getPassword();

        //校验密码和用户名是否为空
        if (zoomenName == null || "".equals(zoomenName.trim()) || chars == null || loginview.getFlag() == 0) {
            JOptionPane.showMessageDialog(loginview, "用户名和密码必填，身份必选");
            return;
        }

        String idNumber = new String(chars);
        System.out.println(zoomenName + ":" + idNumber);

        //动物饲养员登录

        ZoomenService zoomenService = new ZoomenServiceImpl();


        ZoomenEntity zoomenEntity = new ZoomenEntity();
        zoomenEntity.setZoomenName(zoomenName);
        zoomenEntity.setIdNumeber(idNumber);
        boolean flag = zoomenService.validateZoomen(zoomenEntity);


        if (flag) {
            //跳转到动物饲养员的两个主界面并销毁登录界面
            String zoomenName1 = loginview.getUsertxt().getText();

            new ZoomenView(zoomenName1);
            new ZoomenView1(zoomenName1);
            loginview.dispose();

        } else {
            JOptionPane.showMessageDialog(loginview, "用户名和密码错误");
        }
    }

    private void loginLeader() throws SQLException {
        String leaderName = loginview.getUsertxt().getText();
        char[] chars = loginview.getPwdField().getPassword();

        //校验密码和用户名是否为空
        if (leaderName == null || "".equals(leaderName.trim()) || chars == null || loginview.getFlag() == 0) {
            JOptionPane.showMessageDialog(loginview, "用户名和密码必填，身份必选");
            return;
        }

        String idNumber = new String(chars);
        System.out.println(leaderName + ":" + idNumber);

        //园长登录


        LeaderService leaderService = new LeaderServiceImpl();


        LeaderEntity leaderEntity = new LeaderEntity();
        leaderEntity.setLeaderName(leaderName);
        leaderEntity.setIdNumeber(idNumber);
        boolean flag = leaderService.validateLeader(leaderEntity);

        if (flag) {
            //跳转到园长主界面并销毁登录界面

            new LeaderView();
            new LeaderView1();
            loginview.dispose();
        } else {
            JOptionPane.showMessageDialog(loginview, "用户名和密码错误");
        }
    }
}
